from docx import Document
from docx.shared import Inches
from docx.enum.text import WD_ALIGN_PARAGRAPH
# from docx.enum.text import WD_LINE_SPACING
from datetime import date
# import os, re
import random
import json
import numpy as np
import tkinter as tk
from tkinter.ttk import *

def clicked():
    std_name = std_name2.get()
    is_gender = is_gender2.get()
    is_gender_val = is_gender2['values'].index(is_gender)
    course_name = course_name2.get()
    is_paper = is_paper2.get()
    is_paper_val = is_paper2['values'].index(is_paper)
    supervisor_name = supervisor_name2.get()
    supervisor_email = supervisor_email2.get()
    supervisor_email = 'Email: ' + supervisor_email;
    supervisor_phone = supervisor_phone2.get()
    supervisor_phone = 'Phone: ' + supervisor_phone
    
    with open("data.json",mode='r',encoding="utf-8") as file:
        data = json.loads(file.read())
    
    gender1 = ['Mr.', 'Ms.']
    gender2 = ['his', 'her']
    gender3 = ['he', 'she']
    gender4 = ['himself', 'herself']
    
    
    if is_gender_val == 0:
        gender1_val = gender1[0]
        gender2_val = gender2[0]
        gender3_val = gender3[0]
        gender4_val = gender4[0]
    elif is_gender_val == 1:
        gender1_val = gender1[1]
        gender2_val = gender2[1]
        gender3_val = gender3[1]
        gender4_val = gender4[1]
        
    
    data2 = data
    for item in data2:
        jj = 0
        for word in data2[item]:
           data2[item][jj] = word.replace('std_name',std_name)
           data2[item][jj] = data2[item][jj].replace('gender1',gender1_val)
           data2[item][jj] = data2[item][jj].replace('gender2',gender2_val)
           data2[item][jj] = data2[item][jj].replace('gender3',gender3_val)
           data2[item][jj] = data2[item][jj].replace('gender4',gender4_val)
           data2[item][jj] = data2[item][jj].replace('course_name',course_name)
           jj = jj+1
           
    dear = data['dear']
    sen1a = data['sen1a']
    sen1b = data['sen1b']
    sen2a = data['sen2a']
    sen2b = data['sen2b']
    
    link = data["link"]
    
    
    sen_mid0 = data['sen_mid0']
    sen_mid1 = data['sen_mid1']
    sen_mid2a = data['sen_mid2a']
    sen_mid2b = data['sen_mid2b']
    sen_mid3a = data['sen_mid3a']
    sen_mid3b = data['sen_mid3b']
    sen_mid3c = data['sen_mid3c']
    sen_mid3d = data['sen_mid3d']
    sen_mid40 = data['sen_mid40']
    sen_mid4a = data['sen_mid4a']
    
    sen_last0 = data['sen_last0']
    sen_lasta = data['sen_lasta']
    sen_lasta2 = data['sen_lasta2']
    sen_lastb = data["sen_lastb"]
    sen_lastc = data['sen_lastc']
    sen_lastd = data['sen_lastd']
    
    def get_one(mylist):
        while True:
            random.shuffle(mylist)
            for element in mylist:
                yield element
                
    one_sen_lastb = next(get_one(sen_lastb) )         
    one_sen_lastb = one_sen_lastb.replace("link",random.choice(link) )
    
    
    
    document = Document()
    
    school_logo = document.add_picture('school_logo.png', width=Inches(4.25))
    last_paragraph = document.paragraphs[-1]
    last_paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
    
    today = date.today()
    p0 = document.add_paragraph(today.strftime("%B %d, %Y"))
    p1 = document.add_paragraph(random.choice(dear))
    
    p1_1 = document.add_paragraph(random.choice(sen1a))
    p1_1.add_run(' ' + random.choice(sen1b) + '. ')
    
    p1_1.add_run(random.choice(sen2a))
    p1_1.add_run(random.choice(sen2b) + '.')
    
    
    p2_1 = document.add_paragraph(random.choice(sen_mid0))
    p2_1.add_run(' ' + random.choice(sen_mid1))
    
    if is_paper_val == 1: 
        dum1 = np.random.choice(sen_mid2a, 2, replace=False)
        dum2 = np.random.choice(sen_mid2a, 3, replace=False)
        
        p2_1.add_run(' ' + dum1[0] + ' ' + dum1[1])
        p2_1.add_run(' ' + dum2[0] + ' ' + dum2[1] + ' ' + dum2[2])
        
    elif is_paper_val == 0:    
        dum1 = np.random.choice(sen_mid2a, 1, replace=False)
        dum2 = np.random.choice(sen_mid2a, 2, replace=False)

        p2_1.add_run(' ' + dum1[0])
        p2_1.add_run(' ' + dum2[0] + ' ' + dum2[1])
        
        p2_1.add_run(' ' + random.choice(sen_mid3a))
        p2_1.add_run(' ' + random.choice(sen_mid3b))
        p2_1.add_run(' ' + random.choice(sen_mid3c))
        p2_1.add_run(' ' + random.choice(sen_mid3d))
        
    p2_1.add_run(' ' + random.choice(sen_mid40))
    p2_1.add_run(' ' + random.choice(sen_mid4a))
    
    
    plast = document.add_paragraph(random.choice(sen_last0))
    plast.add_run(random.choice(sen_lasta))
    plast.add_run(random.choice(sen_lasta2))
    plast.add_run(one_sen_lastb)
    plast.add_run(random.choice(sen_lastc))
    plast.add_run(random.choice(sen_lastd))
    
    p1_1.alignment = 3 # for left, 1 for center, 2 right, 3 justify 
    p2_1.alignment = 3 # for left, 1 for center, 2 right, 3 justify 
    plast.alignment = 3 # for left, 1 for center, 2 right, 3 justify 
                
    document.add_paragraph()
    
    pend1 = document.add_paragraph('Yours Sincerely,')
    pend2 = document.add_paragraph('')
    pend3 = document.add_paragraph(supervisor_name)
    pend3.add_run().add_break()
    pend3.add_run('Associate Professor').add_break()
    pend3.add_run('Address: No. 555, Liutai Avenue, Wenjiang District, Chengdu,\
    Sichuan, P. R. China 611130').add_break()
    pend3.add_run(supervisor_phone).add_break()
    pend3.add_run(supervisor_email).add_break()
    rand_num = str(random.randint(1,1000000))
    doc_name = 'RL_' + supervisor_name + rand_num + '.docx'
    document.save(doc_name)


#############################################
window = tk.Tk()
window.option_add('*Font', 'Times 14')
window.title("推荐信生成器")

window.geometry('550x250')

lbl_std_name = tk.Label(window, text="你的名字")
lbl_std_name.grid(column=0, row=0)
std_name2 = tk.Entry(window,width=20)
std_name2.insert(0, "Lalala")
std_name2.grid(column=1, row=0)

lbl_gender = tk.Label(window, text="性别")
lbl_gender.grid(column=0, row=1)
is_gender2 = Combobox(window,width=5)
is_gender2['values']= ("男", "女")
is_gender2.current(1)
is_gender2.grid(column=1, row=1)

lbl_course_name = tk.Label(window, text="课程名称")
lbl_course_name.grid(column=0, row=2)
course_name2 = tk.Entry(window,width=25)
course_name2.insert(0, "International Economics")
course_name2.grid(column=1, row=2)


lbl_paper = tk.Label(window, text="有没有写课程论文?")
lbl_paper.grid(column=0, row=3)
is_paper2 = Combobox(window,width=5)
is_paper2['values']= ("有", "无")
is_paper2.current(0)
is_paper2.grid(column=1, row=3)

lbl_supervisor_name = tk.Label(window, text="老师名字")
lbl_supervisor_name.grid(column=0, row=4)
supervisor_name2 = tk.Entry(window,width=20)
supervisor_name2.insert(0, "Tungtung Cha")
supervisor_name2.grid(column=1, row=4)

lbl_supervisor_email= tk.Label(window, text="老师邮箱")
lbl_supervisor_email.grid(column=0, row=5)
supervisor_email2 = tk.Entry(window,width=25)
supervisor_email2.insert(0, "hello123@swufe.edu.cn")
supervisor_email2.grid(column=1, row=5)

lbl_supervisor_phone= tk.Label(window, text="老师电话")
lbl_supervisor_phone.grid(column=0, row=6)
supervisor_phone2 = tk.Entry(window,width=20)
supervisor_phone2.insert(0, "(+86)12345678910")
supervisor_phone2.grid(column=1, row=6)


btn = tk.Button(master = window, text="按我生成", command=clicked, font = ("Times New Roman", 15))
btn.grid(column=100, row=100)

window.mainloop()